const app = getApp();
var picRoot = app.data.picRoot;
var pageNo = 1;
var pageSize = 10;
var util = require('../../utils/util.js');
var url = app.data.url;
var platFormUrl = app.data.platFormUrl
import Dialog from '/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    envPic: ["1", "2", "3"],
    nav: [],
    itemDetailList: [],
    windowWidth:0,
    keyValueList: [],
    addr: '',
    lat: '',
    lot: '',
    merchantName: '',
    merchantId: '',
    phone: '',
    coverIcon: '',
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    classId: ""
  },
  /**跳转到商家详情页 */
  toDetailPage: function (e) {
    console.log("hkjkjk");
  },
  /**
   * 
   * 导航栏点击响应事件
   * 
   * 1.点击项目出现下滑线
   * 2.点击项目，导航栏滚动，使得项目出现再屏幕固定位置
   * 
   */
  currentNav: function (e) {
    let _that = this;
    let index = e.currentTarget.dataset.index;
    let scrollLeft = e.currentTarget.offsetLeft - (this.data.windowWidth * 0.9) / 2;
    this.setData({
      current: index,
      scrollLeft: scrollLeft
    })
    // 查询商品
    console.log(e.currentTarget.dataset);
    let classId = e.currentTarget.dataset.id;
    let merchantId = _that.data.merchantId;
    _that.queryItemDetailData(pageNo, pageSize, merchantId, classId, false, null);
  },
  /**跳转商品明细页面 */
  toMerchantItem: function (option) {
    let _that = this;
    console.info(option);
    let merchantId = _that.data.merchantId;
    let id = option.currentTarget.dataset.id;
    let name = option.currentTarget.dataset.name;
    let originalprice = option.currentTarget.dataset.originalprice;
    let discountprice = option.currentTarget.dataset.discountprice;
    let payType = option.currentTarget.dataset.paytype;
    wx.navigateTo({
      url: '/pages/merchant-item-detail/index?merchantId=' + merchantId + "&id=" + id + "&name=" + name + "&originalprice=" + originalprice + "&discountprice=" + discountprice + "&payType=" + payType
    })
  },
  /**
   * 加载商品 flag 标志是否第一页 false：第1页，true 非第1页
   */
  queryData: function (pageNo, pageSize, merchantId, flag, auth) {
    let _that = this;
    // 查询商品分类
    wx.request({
      url: platFormUrl + '/MerchantApi/client/noAuth/queryMerchantCategory',
      data: {
        id: merchantId
      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'Authorization': auth
      },
      success: function (res) {
        if (res.data.code == 200) {
          console.log(res.data.data);
          _that.setData({
            nav: res.data.data
          })
          // 初始化查询全部商品
          _that.queryItemDetailData(pageNo, pageSize, merchantId, false, flag, auth);
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }
      },
      fail: function (res) {
        console.info('submit fail');
      },
      complete: function (res) {
        console.info('submit complete');
      }
    })
  },

  queryItemDetailData: function (pageNo, pageSize, merchantId, classId, flag, auth) {
    let _that = this;
    let dataVal = {};
    if (classId) {
      // 按分类查询商品
      dataVal = {
        pageNo: pageNo,
        pageSize: pageSize,
        merchantId: merchantId,
        classId: classId
      }
    } else {
      // 查询全部商品
      dataVal = {
        pageNo: pageNo,
        pageSize: pageSize,
        merchantId: merchantId,
        classId: 0
      }
    }
    wx.request({
      url: platFormUrl + '/MerchantApi/client/noAuth/pageListItemDetail',
      data: dataVal,
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'Authorization': auth
      },
      success: function (res) {
        if (res.data.code == 200) {
          console.log("pageListItemDetail");
          console.log(res.data.data);
          if (!flag) {
            _that.setData({
              itemDetailList: res.data.data.pageList
            })
          } else {
            if (res.data.data.pageList.length > 0) {
              _that.setData({
                itemDetailList: _that.data.itemDetailList.concat(res.data.data.pageList)
              })
            }
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }
      },
      fail: function (res) {
        console.info('初始化查询全部商品 submit fail');
      },
      complete: function (res) {
        console.info('初始化查询全部商品 submit complete');
      }
    })
  },

  toMerhcantImg: function (e) {
    let that = this;
    // 跳转到商家img页面

  },

  /**
   * 加载商家明细
   */
  queryMerhcantDetail: function (merchantId, auth) {
    let that = this;
    console.log("queryMerhcantDetail: " + auth);
    wx.request({
      url: platFormUrl + '/MerchantApi/client/noAuth/queryMerchantDetail',
      data: {
        id: merchantId
      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'Authorization': auth
      },
      success: function (res) {
        if (res.data.code == 200) {
          console.log("商家-=------");
          console.log(res);
          let detail = res.data.data;
          that.setData({
            addr: res.data.data.address,
            lat: res.data.data.latitude,
            lot: res.data.data.longitude,
            merchantName: res.data.data.name,
            phone: res.data.data.phone,
            merchantCoverImg: res.data.data.coverIcon,
            keyValueList: res.data.data.detailList,
            coverIcon: picRoot + res.data.data.coverIcon
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }
      },
      fail: function (res) {
        console.info('初始化商家详情 submit fail');
      },
      complete: function (res) {
        console.info('初始化商家详情 submit complete');
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    console.log(options);
    let merchantId = options.merchantId;
    console.log(merchantId);
    let auth;
    // 加载商家明细
    that.queryMerhcantDetail(merchantId, auth);
    // 加载商品分类、商品
    that.queryData(pageNo, pageSize, merchantId, 1, auth);

    that.setData({
      merchantId: merchantId
    })
  },


  makeCall: function (option) {
    var phone = option.currentTarget.dataset.phone;
    console.log(option);
    wx.makePhoneCall({
      phoneNumber: phone,
      success: function () {
        console.log("成功拨打电话")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })

  },

  toShowDatePage: function () {

  },

  /***
   * 调起导航
   */
  toLocation: function (options) {
    let _that = this;
    let lat = _that.data.lat;
    let lot = _that.data.lot;
    let addr = _that.data.addr;
    let name = _that.data.name;
    wx.openLocation({
      longitude: Number(lot),
      latitude: Number(lat),
      name: name,
      address: addr
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let _that = this;
    let data = JSON.stringify(_that.data.onLoadData);
    let queryType = _that.data.queryType;
    let merchantName = _that.data.name;
    return {
      path: 'pages/hairdressing/index?data=' + data + "&queryType=" + queryType,
      title: merchantName
    }
  }
})